namespace Xmef.Library
{
    using System;
    using System.Windows.Forms;

    public static class ControlHelper
    {
        public static void Action<T>(T control, Action<T> method) where T: Control
        {
            Invoke.Action<T>(control, method);
        }

        private static void Action(Control control, Procedure method)
        {
            Invoke.Action(control, method);
        }

        public static void Disable(Control control)
        {
            if (control != null)
            {
                Action<Control>(control, delegate {
                    control.Enabled = false;
                });
            }
        }

        public static void Enable(Control control)
        {
            if (control != null)
            {
                Action<Control>(control, delegate
                {
                    control.Enabled = true;
                });
            }
        }

        private static class Invoke
        {
            public static void Action(Control control, ControlHelper.Procedure method)
            {
                if (control.InvokeRequired)
                {
                    control.Invoke(method);
                }
                else
                {
                    method();
                }
            }

            public static void Action<T>(T control, Action<T> method) where T: Control
            {
                if (control.InvokeRequired)
                {
                    control.Invoke(method, new object[] { control });
                }
                else
                {
                    method(control);
                }
            }
        }

        private delegate void Procedure();
    }
}

